package com.example.bgmachine.bgmachine.goods;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.bgmachine.bgmachine.R;
import com.example.bgmachine.bgmachine.homes.HomePages;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class GoodFragment extends Fragment {
    private Context mContext = null;
    private ListView homepages_bottom_left_listvew = null;
    private ArrayList<HomepageLeftMenuItemData> mHomepageLeftMenuItemData = new ArrayList<HomepageLeftMenuItemData>();
    private HomepageListLeftMenuAdapter mHomepageListLeftMenuAdapter = null;


    private GridView homepages_bottom_right_gridvew = null;
    private ArrayList<HomepageGridRightMenuItemData> mHomepageGridRightMenuItemData = new ArrayList<HomepageGridRightMenuItemData>();
    private HomepageGridRightMenuAdapter mHomepageGridRightMenuAdapter = null;

    public GoodFragment() {

    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container, @NonNull Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.goods_fragment_layout, container, false);
        homepages_bottom_left_listvew = view.findViewById(R.id.homepages_bottom_left_listvew);
        homepages_bottom_right_gridvew = view.findViewById(R.id.homepages_bottom_right_gridvew);
        mContext =view.getContext();
        mHomepageGridRightMenuAdapter = new HomepageGridRightMenuAdapter(mContext, mHomepageGridRightMenuItemData);

        homepages_bottom_right_gridvew.setAdapter(mHomepageGridRightMenuAdapter);


        homepages_bottom_right_gridvew.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                HomepageGridRightMenuItemData  itema =   mHomepageGridRightMenuItemData.get(position);
                HomepageLeftMenuItemData   itemb = mHomepageLeftMenuItemData.get(LeftSelectindex.getLeftSelectindex().mLeftSelectIndex);

                HomePages.mHomePages.setSpecifPopuView(itemb,itema,itema.getMenuNames(),itema.getContents());

            }
        });
        homepages_bottom_left_listvew = view.findViewById(R.id.homepages_bottom_left_listvew);

        homepages_bottom_left_listvew.setDivider(null);
        mHomepageListLeftMenuAdapter = new HomepageListLeftMenuAdapter(view.getContext(), mHomepageLeftMenuItemData);

        homepages_bottom_left_listvew.setAdapter(mHomepageListLeftMenuAdapter);


        homepages_bottom_left_listvew.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                LeftSelectindex.getLeftSelectindex().mLeftSelectIndex = position;
                mHomepageListLeftMenuAdapter.notifyDataSetChanged();

                changeLeftMenuInfors();
            }
        });
        return view;
    }

    public void initLeftMenu(String infors) {
        mHomepageLeftMenuItemData.clear();
        Log.d("bbb==", infors);
        try {
            JSONArray jsonArrya = new JSONArray(infors);

            for(int j = 0 ;j < jsonArrya.length() ; j++){
                JSONObject itema = jsonArrya.getJSONObject(j);
                String firstMenuid = itema.opt("firstMenuid").toString();
                String firstMenuNames = itema.opt("firstMenuNames").toString();
                JSONArray children = itema.optJSONArray("children");
                Log.d("aaa", "initLeftMenu0-children="+children);
                Log.d("aaa", "initLeftMenu0-secondM------+++++-------");

                for(int ka = 0 ; ka < children.length() ;ka++){
                    JSONObject iatema1 = children.getJSONObject(ka);
                    String secondMenuNames = iatema1.opt("secondMenuNames").toString();
                    Log.d("aaa", "initLeftMenu0-secondMenuNames="+secondMenuNames);

                }
                Log.d("aaa", "initLeftMenu0-secondM-------------");

                mHomepageLeftMenuItemData.add(new HomepageLeftMenuItemData(Integer.parseInt(firstMenuid), firstMenuNames, children.toString()));

            }


        } catch (JSONException e) {
            e.printStackTrace();

        }
        Log.d("bbb==", "9");
        if (mHomepageLeftMenuItemData.size() > 0) {
            LeftSelectindex.getLeftSelectindex().mLeftSelectIndex = 0;
            Log.d("bbb==", "11");
        }

        Log.d("bbb==", "12");
        if (mHomepageListLeftMenuAdapter != null) {
            Log.d("bbb==", "13");
            mHomepageListLeftMenuAdapter.notifyDataSetChanged();
            Log.d("bbb==", "14");
        }

        Log.d("bbb==", "15");
        changeLeftMenuInfors();
    }

    public void changeLeftMenuInfors() {
        if (LeftSelectindex.getLeftSelectindex().mLeftSelectIndex < 0) {
            return;
        }

        mHomepageGridRightMenuItemData.clear();

        if (LeftSelectindex.getLeftSelectindex().mLeftSelectIndex >= mHomepageLeftMenuItemData.size()) {
            LeftSelectindex.getLeftSelectindex(). mLeftSelectIndex = mHomepageLeftMenuItemData.size() - 1;
        }
        String menuNames = mHomepageLeftMenuItemData.get(LeftSelectindex.getLeftSelectindex().mLeftSelectIndex).getMenuNames();
        int menuId = mHomepageLeftMenuItemData.get(LeftSelectindex.getLeftSelectindex().mLeftSelectIndex).getMenuId();
        String contents = mHomepageLeftMenuItemData.get(LeftSelectindex.getLeftSelectindex().mLeftSelectIndex).getContents();
        try {
            JSONArray secondMenu = new JSONArray(contents);
            Log.d("aaa", "changeLeftMenuInfors: ="+secondMenu.toString());
            for (int j = 0; j < secondMenu.length(); j++) {
                JSONObject itema = secondMenu.getJSONObject(j);
                Log.d("aaa", "changeLeftMenuInfors: itema="+itema.toString());
                String secondMenuid = itema.opt("secondMenuid").toString();
                String secondMenuNames = itema.opt("secondMenuNames").toString();
                String secondMenuReserve = itema.opt("secondMenuReserve").toString();
                Log.d("aaa", "secondMenuNames--aa=="+secondMenuNames.toString());

                JSONArray children = itema.optJSONArray("children");
                if (secondMenuReserve.length() <= 0 || secondMenuReserve.contains("xlsx")) {
                    Log.d("aaa", "secondMenuNames--aa=0=");

                    mHomepageGridRightMenuItemData.add(new HomepageGridRightMenuItemData(Integer.parseInt(secondMenuid), secondMenuNames, children.toString()));

                } else {
                    Log.d("aaa", "secondMenuNames--aa=2=");

                    Log.d("aaa", "secondMenuReservesecondMenuReserve:="+secondMenuReserve);
                    mHomepageGridRightMenuItemData.add(new HomepageGridRightMenuItemData(Integer.parseInt(secondMenuid), secondMenuNames, secondMenuReserve, children.toString()));

                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
            Log.d("aaa", "secondMenuNames--aa=1=");

        }
//        for (int i = 0; i < 20; i++) {
//            int firstMenuId = menuId * 10 + i;
//            String firstMenuName = menuNames + "*饮料*" + String.valueOf(i);
//            mHomepageGridRightMenuItemData.add(new HomepageGridRightMenuItemData(firstMenuId, firstMenuName));
//        }
        Log.d("aaa", "secondMenuNames--aa=mHomepageGridRightMenuItemData="+mHomepageGridRightMenuItemData.size());


        mHomepageGridRightMenuAdapter.notifyDataSetChanged();
    }

}
